package cn.zjq.one;


import cn.zjq.stu.Student;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class StuManagement {
    //用来保存学生对象
    private static List<Student> studentList = new ArrayList<>();

    //新增学生
    public static boolean addStudent(Student student) {
        for (int index = 0; index < studentList.size(); index++) {
            Student stu = studentList.get(index);
            if (stu.getId() == student.getId()) {
                return false;
            }
        }
        return studentList.add(student);
    }

    //根据学号(唯一性)删除集合中指定的学生对象
    public static boolean delStudentById(int id) {
        int index = -1;
        for (int i = 0; i < studentList.size(); i++) {
            Student student = studentList.get(i);
            if (id == student.getId()) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            studentList.remove(index);
            return true;
        } else {
            return false;
        }
    }

    //根据学号修改学生信息
    public static boolean updStudentById(Student student) {
        int index = -1;
        for (int i = 0; i < studentList.size(); i++) {
            cn.zjq.stu.Student stu = studentList.get(i);
            if (stu.getId() == student.getId()) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            studentList.set(index, student);
            return true;
        } else {
            return false;
        }
    }

    //返回所有的学生对象
    public static List<Student> getAllStudent() {
        return studentList;
    }

    //根据学号获取单个学生详细信息
    public static Student getStudentById(int id) {
        int index = -1;
        for (int i = 0; i < studentList.size(); i++) {
            Student student = studentList.get(i);
            if (id == student.getId()) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            return studentList.get(index);
        } else {
            return null;
        }
    }

    //根据学生姓名模糊查询
    public List<Student> searchStudentByLike(String name) {
        List<Student> tempList = new ArrayList<>();
        for (Student student : studentList) {
            if (student.getName().indexOf(name) != -1) {
                tempList.add(student);
            }
        }
        return tempList;
    }

    //应用程序入口方法
    public static void main(String[] args) {
        Student student1 = new Student("张三", 19, "111111", 111);
        Student student2 = new Student("李四", 20, "222222", 222);
        Student student3 = new Student("王五", 21, "333333", 333);
        studentList.add(student1);
        studentList.add(student2);
        studentList.add(student3);

        Scanner input = new Scanner(System.in);
        boolean flag = true;
        do {
            System.out.println("************欢迎来到北大青鸟学生管理系统************");
            System.out.println("请选择您的操作：");
            System.out.println("1.增加学员信息");
            System.out.println("2.修改学员信息");
            System.out.println("3.删除学员信息");
            System.out.println("4.显示全部学员");
            System.out.println("0.退出");
            int x = input.nextInt();
            switch (x) {
                case 1:
                    System.out.println("请输入学号：");
                    int id = input.nextInt();
                    System.out.println("请输入姓名：");
                    String name = input.next();
                    System.out.println("请输入年龄：");
                    int age = input.nextInt();
                    System.out.println("请输入身份证号：");
                    String idnum = input.next();
                    cn.zjq.stu.Student student = new cn.zjq.stu.Student(name, age, idnum, id);
                    boolean result = addStudent(student);
                    if (result) {
                        System.out.println("新增学生信息成功！");
                    } else {
                        System.out.println("新增学生信息失败！");
                    }
                    break;
                case 2:
                    cn.zjq.stu.Student stu2 = new cn.zjq.stu.Student();
                    System.out.println("请输入将要修改信息的学员学号：");
                    stu2.setId(input.nextInt());
                    System.out.println("请输入更改后的姓名：");
                    stu2.setName(input.next());
                    System.out.println("请输入更改后的年龄：");
                    stu2.setAge(input.nextInt());
                    System.out.println("请输入更改后的身份证号：");
                    stu2.setIdnum(input.next());
                    boolean l1 = updStudentById(stu2);
                    if (l1) {
                        System.out.println("学生信息修改成功！");
                    } else {
                        System.out.println("学生信息修改失败！");
                    }
                    break;
                case 3:
                    System.out.println("请输入将要删除的学员学号：");
                    int id1 = input.nextInt();
                    boolean l = delStudentById(id1);
                    if (l) {
                        System.out.println("学生信息删除成功！");
                    } else {
                        System.out.println("学生信息删除失败！");
                    }
                    break;
                case 4:

                    List<cn.zjq.stu.Student> students = getAllStudent();
                    Iterator<cn.zjq.stu.Student> it = students.iterator();
                    System.out.println("学号\t姓名\t年龄\t身份证号");
                    System.out.println("-----------------------------------------");
                    while (it.hasNext()) {
                        Student stu = it.next();
                        System.out.println(stu.getId() + "\t\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getIdnum());
                        System.out.println("-----------------------------------------");
                    }
                    break;
                case 0:
                    System.out.println("谢谢使用本程序，欢迎下次光临。。。");
                    flag = false;
                    break;
            }
        }
        while (flag);
    }
}
